
enum INFO {
    //% block="天气"
    1,
    //% block="地区"
    2,
    //% block="时区"
    3,
    //% block="温度"
    4
}




// % color="#AA278D" iconWidth=50 iconHeight=40
namespace ESP8266231001 {
    //% block="Arduino软串口的RX,TX引脚:[RX]，[TX]，让ESP8266连接WIFI:[STR1],[STR2]" blockType="command"
    //% TX.shadow="dropdown" TX.options="PIN_DigitalWrite" 
    //% RX.shadow="dropdown" RX.options="PIN_DigitalWrite"  
    //% STR1.shadow="string" STR1.defl=WIFI名称
    //% STR2.shadow="string" STR2.defl=WIFI密码
    //% STR3.shadow="string" STR3.defl=心知天气密匙
    export function ConnectWIFI(parameter: any, block: any) {
        let ssid = parameter.STR1.code
        let pass = parameter.STR2.code 
        ssid = "\\" + ssid.slice(0, -1) + "\\\""  ;//插入转义字符
        pass = "\\" + pass.slice(0, -1) + "\\\""  ;//插入转义字符
        let TX = parameter.TX.code;
        let RX = parameter.RX.code;

        Generator.addInclude('ESP8266231001', '#include <SoftwareSerial.h>');
        Generator.addInclude('Analysis', '#include "Analysis.h"');
        Generator.addObject(`mySerial`, `SoftwareSerial`, `mySerial(${RX},${TX});`);
        Generator.addSetup(`Serial.begin`, `Serial.begin(9600);`);
        Generator.addSetup(`mySerial.begin`, `mySerial.begin(9600);`);
        Generator.addSetup(`delay`, `delay(1000);`);

        Generator.addCode(`
  mySerial.println("AT+RST");
  while (!mySerial.available());
  Reiceve(mySerial);
  mySerial.println("AT+CWMODE=1");
  while (!mySerial.available());
  Reiceve(mySerial);
  mySerial.println("AT+CWJAP=${ssid},${pass}");
  while (!mySerial.available());
  Reiceve(mySerial);
  delay(5000);
  mySerial.println("AT+CIPSTART=\\"TCP\\",\\"api.seniverse.com\\",80");
  while (!mySerial.available());
  Reiceve(mySerial);
  mySerial.println("AT+CIPMODE=1");//开启透传
  while (!mySerial.available());
  Reiceve(mySerial);
  mySerial.println("AT+CIPSEND");//开始透传
  while (!mySerial.available());
  Reiceve(mySerial);

        `);

    }

    //% block="从心知天气获取信息，密钥为:[STR]，地点为:[POS]" blockType="command"
    //% STR.shadow="string" STR.defl=S0GQwFP0zq1hqTbSq
    //% POS.shadow="string" POS.defl=Shanghai
    export function RequestWeather(parameter: any, block: any) {
        let key = parameter.STR.code
        let position = parameter.POS.code

        let sendmessage = "GET https://api.seniverse.com/v3/weather/now.json?key=" + key.slice(1,-1) + "&location=" + position.slice(1,-1) + "&language=en&unit=c"
        
        Generator.addCode(`mySerial.println("${sendmessage}");`);
    }

    //% block="从串口显示 [INFO] 信息" blockType="command"
    //% INFO.shadow="dropdownRound" INFO.options="INFO" INFO.defl="INFO.1"
    export function println(parameter: any, block: any) {
        let info = parameter.INFO.code
        let displayinfo = 100;

        Generator.addCode(`Get_info(mySerial,${info});`);
        Generator.addInclude('Analysis', '#include "Analysis.h"');
    }

    //% block="when press [BUTTON]" blockType="hat"
    //% BUTTON.shadow="dropdown" BUTTON.options="BTN" BUTTON.defl="BTN.A"
    // export function buttonPress(parameter: any, block: any) {
    //     let button = parameter.BUTTON.code;
    //     button = replace(button);
    //     let name = 'button' + button + 'PressCallback';
    //     if(Generator.board === 'microbit'){
    //         Generator.addEvent(name, "void", name, "", true);
    //         Generator.addSetup(block.id, `onEvent(ID_BUTTON_${button}, PRESS, ${name});`, false);
    //     }else{
    //         Generator.addInclude('MPython', '#include <MPython.h>');
    //         Generator.addEvent(name, "void", name, "", true);
    //         Generator.addSetupMainTop("mPython.begin", "mPython.begin();");
    //         Generator.addSetup(`button${button}.setPressedCallback`, `button${button}.setPressedCallback(${name});`);
    //     }
    // }



    // //% block="show [STR] at x [X] y [Y]" blockType="command"
    // //% STR.shadow="string" STR.defl=hello
    // //% X.shadow="range" X.params.min=0 X.params.max=127 X.defl=0
    // //% Y.shadow="range" Y.params.min=0 Y.params.max=63 Y.defl=0
    // export function print(parameter: any, block: any) {
    //     let str = parameter.STR.code
    //     let x = parameter.X.code
    //     let y = parameter.Y.code
    //     Generator.addInclude('oled12864', '#include <oled12864.h>');
    //     Generator.addObject(`myoled`, `OLED_12864`, `myoled;`);
    //     Generator.addSetup(`myoled.begin`, `myoled.begin();`);
    //     Generator.addCode(`myoled.setCursor(${x}, ${y});\n\tmyoled.print(${str});`);
    // }

    // //% block="display QR code [STR] at x [X] y [Y] with size [SIZE]" blockType="command"
    // //% STR.shadow="string" STR.defl=http://mindplus.cc
    // //% X.shadow="range" X.params.min=0 X.params.max=127 X.defl=0
    // //% Y.shadow="range" Y.params.min=0 Y.params.max=63 Y.defl=0
    // //% SIZE.shadow="dropdownRound" SIZE.options="SIZE" SIZE.defl="SIZE.2"
    // export function qrcode(parameter: any, block: any) {
    //     let str = parameter.STR.code
    //     let x = parameter.X.code
    //     let y = parameter.Y.code
    //     let size = parameter.SIZE.code
    //     Generator.addInclude('oled12864', '#include <oled12864.h>');
    //     Generator.addObject(`myoled`, `OLED_12864`, `myoled;`);
    //     Generator.addSetup(`myoled.begin`, `myoled.begin();`);
    //     Generator.addCode(`myoled.qrcode(${x}, ${y}, ${str}, ${size});`);
    // }

    // //% block="set the line width to [WIDTH] pixels" blockType="command"
    // //% WIDTH.shadow="range" WIDTH.params.min=1 WIDTH.params.max=128 WIDTH.defl=1
    // export function setLineWidth(parameter: any, block: any) {
    //     let width = parameter.WIDTH.code
    //     Generator.addInclude('oled12864', '#include <oled12864.h>');
    //     Generator.addObject(`myoled`, `OLED_12864`, `myoled;`);
    //     Generator.addSetup(`myoled.begin`, `myoled.begin();`);
    //     Generator.addCode(`myoled.setLineWidth(${width});`);
    // }

    // //% block="get the line width" blockType="reporter"
    // export function getLineWidth(parameter: any, block: any) {
    //     Generator.addInclude('oled12864', '#include <oled12864.h>');
    //     Generator.addObject(`myoled`, `OLED_12864`, `myoled;`);
    //     Generator.addSetup(`myoled.begin`, `myoled.begin();`);
    //     Generator.addCode(`myoled.getLineWidth()`);
    // }

    // //% block="button [BUTTON] is pressed?" blockType="boolean"
    // //% Flag.shadow="boolean"
    // //% BUTTON.shadow="dropdown" BUTTON.options="BTN" BUTTON.defl="BTN.A"
    // export function buttonIsPressed(parameter: any, block: any) {
    //     let button = parameter.BUTTON.code.replace("+", "");
    //     let code;
    //     if(Generator.board === 'microbit'){
    //         if (button === 'A') {
    //             code = `Button_A.isPressed() && !Button_B.isPressed()`;
    //         } else if (button === 'B') {
    //             code = `Button_B.isPressed() && !Button_A.isPressed()`;
    //         } else {
    //             code = `Button_AB.isPressed()`;
    //         }
    //         Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);
    //     }else{
    //         code = `button${button}.isPressed()`;
    //         Generator.addInclude('MPython', '#include <MPython.h>');
    //         Generator.addSetupMainTop("mPython.begin", "mPython.begin();");
    //         Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);
    //     }
    // }

    // //% block="not [Flag]" blockType="boolean"
    // //% Flag.shadow="boolean"
    // export function notTrue(parameter: any) {
    //     console.log("notTrue==", parameter);
    //     let code: string = '!' + (parameter.Flag.code || 'false') + '';
    //     Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);
    // }

    // function replace(str :string) {
    //     return str.replace("+", "");
    // }
}
